package com.pig4cloud.pig.module.controller.terminal;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.terminals.TerminalsCheckRecord;
import com.pig4cloud.pig.module.service.terminals.ITerminalsCheckRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 巡检记录Controller
 *
 * @author ruoyi
 * @date 2022-04-29
 */
@Validated
@Api(value = "巡检记录控制器", tags = {"巡检记录管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/terminal/check/record")
public class TerminalsCheckRecordController {

    private final ITerminalsCheckRecordService terminalsCheckRecordService;

    /**
     * 查询巡检记录列表
     */
    @ApiOperation("查询巡检记录列表")
    @PreAuthorize("@pms.hasPermission('module:terminalCheckRecord:list')")
    @GetMapping("/list")
    public TableDataInfo<TerminalsCheckRecord> list(@Validated TerminalsCheckRecord terminalsCheckRecord) {
        QueryWrapper<TerminalsCheckRecord> qw = new QueryWrapper<>();

        TableDataInfo<TerminalsCheckRecord> td = PageUtils.buildDataInfo(terminalsCheckRecordService.page(PageUtils.buildPage(),qw));
        List<TerminalsCheckRecord> list = td.getData();
        return td;
    }

    /**
     * 导出巡检记录列表
     */
    @ApiOperation("导出巡检记录列表")
    @PreAuthorize("@pms.hasPermission('module:terminalCheckRecord:export')")
    @SysLog("巡检记录")
    @GetMapping("/export")
    public List export(@Validated TerminalsCheckRecord terminalsCheckRecord) {
        List<TerminalsCheckRecord> list = terminalsCheckRecordService.list();
        // ExcelUtil<TerminalsCheckRecord> util = new // ExcelUtil<TerminalsCheckRecord>(TerminalsCheckRecord.class);
        return  list;// util.exportExcel(list, "巡检记录");
    }

    /**
     * 获取巡检记录详细信息
     */
    @ApiOperation("获取巡检记录详细信息")
    @PreAuthorize("@pms.hasPermission('module:terminalCheckRecord:query')")
    @GetMapping("/{id}")
    public R<TerminalsCheckRecord> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        TerminalsCheckRecord terminalsCheckRecord = terminalsCheckRecordService.getById(id);
        return R.ok(terminalsCheckRecord);
    }

    /**
     * 新增巡检记录
     */
    @ApiOperation("新增巡检记录")
    @PreAuthorize("@pms.hasPermission('module:terminalCheckRecord:add')")
    @SysLog("巡检记录")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody TerminalsCheckRecord terminalsCheckRecord) {
        return R.okByRow(terminalsCheckRecordService.save(terminalsCheckRecord) ? 1 : 0);
    }

    /**
     * 修改巡检记录
     */
    @ApiOperation("修改巡检记录")
    @PreAuthorize("@pms.hasPermission('module:terminalCheckRecord:edit')")
    @SysLog("巡检记录")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody TerminalsCheckRecord terminalsCheckRecord) {
        return R.okByRow(terminalsCheckRecordService.updateById(terminalsCheckRecord) ? 1 : 0);
    }

    /**
     * 删除巡检记录
     */
    @ApiOperation("删除巡检记录")
    @PreAuthorize("@pms.hasPermission('module:terminalCheckRecord:remove')")
    @SysLog("巡检记录")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(terminalsCheckRecordService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
